/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package txtdao;

import contatoplugin.ContatoAbstract;
import daobase.IDao;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import usuarioplugin.UsuarioAbstract;

/**
 *
 * @author Bush
 */
public class SerializacaoTxt implements IDao {

    private File arquivo = new File("src/pessoas/data/contatos.txt");
    private File arquivoInvalido = new File("src/pessoas/data/contatosInvalidos.txt");
    private File arquivoUsuarios = new File("src/pessoas/data/usuarios.txt");

    @Override
    public void gravarContato(ContatoAbstract cnt) {
        try {
            FileWriter w = new FileWriter(arquivo, true);
            BufferedWriter bf = new BufferedWriter(w);
            bf.write(cnt.toString());
            bf.newLine();
            bf.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    @Override
    public void liparGravarContatos(ArrayList al) {
        ArrayList<ContatoAbstract> c = al;
        try {
            FileWriter w = new FileWriter(arquivo, false);
            BufferedWriter bf = new BufferedWriter(w);
            for (int i = 0; i < c.size(); i++) {
                bf.write(c.get(i).toString());
                bf.newLine();
            }
            bf.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    @Override
    public ArrayList lerContatos() {
        try {
            Scanner scan = new Scanner(arquivo);
            ArrayList<Object> contatos = new ArrayList<>();
            while (scan.hasNextLine()) {
                String linha = scan.nextLine();
                Scanner scanline = new Scanner(linha);
                scanline.useDelimiter(",");
                while (scanline.hasNext()) {
                    String nome = scanline.next();
                    String sobrenome = scanline.next();
                    String telefone = scanline.next();
                    String uf = scanline.next();
                    String operadora = scanline.next();
                    contatos.add(new Contato(nome, sobrenome, telefone, uf, operadora));
                }
            }
            arquivo.delete();
            return contatos;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return null;
    }

    @Override
    public void gravarContatosInvalido(ContatoAbstract cnt) {
        try {
            FileWriter w = new FileWriter(arquivoInvalido, true);
            BufferedWriter bf = new BufferedWriter(w);
            bf.write(cnt.toString());
            bf.newLine();
            bf.close();
        } catch (IOException ex) {
        }
    }

    @Override
    public void limpaGravaContatoInvalido(ArrayList al) {
        try {
            FileWriter w = new FileWriter(arquivoInvalido, false);
            BufferedWriter bf = new BufferedWriter(w);
            for (int i = 0; i < al.size(); i++) {
                bf.write(al.get(i).toString());
                bf.newLine();
            }
            bf.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    @Override
    public ArrayList lerContatosInvalido() {
        try {
            Scanner scan = new Scanner(arquivoInvalido);
            ArrayList<Object> contatos = new ArrayList<>();
            while (scan.hasNextLine()) {
                String linha = scan.nextLine();
                Scanner scanline = new Scanner(linha);
                scanline.useDelimiter(",");
                while (scanline.hasNext()) {
                    String nome = scanline.next();
                    String sobrenome = scanline.next();
                    String telefone = scanline.next();
                    String uf = scanline.next();
                    String operadora = scanline.next();
                    contatos.add(new Contato(nome, sobrenome, telefone, uf, operadora));
                }
            }
            arquivoInvalido.delete();
            return contatos;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return null;
    }

    @Override
    public ArrayList carregaUsuarios() {
        Scanner scan;
        ArrayList<Usuario> usuarios = new ArrayList<Usuario>();
        try {
            scan = new Scanner(arquivoUsuarios);            
            while (scan.hasNextLine()) {
                String linha = scan.nextLine();
                Scanner scanline = new Scanner(linha);
                scanline.useDelimiter(",");
                while (scanline.hasNext()) {
                    String nomeUsuario = scanline.next();
                    String senhaUsuario = scanline.next();
                    usuarios.add(new Usuario(nomeUsuario, senhaUsuario));
                }
            }
            arquivoUsuarios.delete();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SerializacaoTxt.class.getName()).log(Level.SEVERE, null, ex);
        }
        return usuarios;
    }

    @Override
    public void gravaUsuario(UsuarioAbstract u) {
        FileWriter w;
        try {
            w = new FileWriter(arquivoUsuarios, true);
            BufferedWriter bf = new BufferedWriter(w);
            bf.write(u.toString());
            bf.newLine();
            bf.close();
        } catch (IOException ex) {
            Logger.getLogger(SerializacaoTxt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void limpaGravaUsuario(ArrayList u) {
        FileWriter w;
        try {
            w = new FileWriter(arquivoUsuarios, false);
            BufferedWriter bf = new BufferedWriter(w);
            for (int i = 0; i < u.size(); i++) {
                bf.write(u.get(i).toString());
                bf.newLine();
            }
            bf.close();
        } catch (IOException ex) {
            Logger.getLogger(SerializacaoTxt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
